package com.itboy.mall.wms.waresku.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.wms.waresku.vo.MallWareSkuVo;
import com.itboy.mall.wms.waresku.bo.MallWareSkuQueryBo;
import com.itboy.mall.wms.waresku.bo.MallWareSkuAddBo;
import com.itboy.mall.wms.waresku.bo.MallWareSkuEditBo;
import com.itboy.mall.wms.waresku.service.IMallWareSkuService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商品库存Controller
 *
 * @author itboy
 * @date 2021-08-10
 */
@Validated
@Api(value = "商品库存控制器", tags = {"商品库存管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/wareSku")
public class MallWareSkuController extends BaseController {

    private final IMallWareSkuService iMallWareSkuService;

    /**
     * 查询商品库存列表
     */
    @ApiOperation("查询商品库存列表")
    @PreAuthorize("@ss.hasPermi('mall:wareSku:list')")
    @GetMapping("/list")
    public TableDataInfo<MallWareSkuVo> list(@Validated MallWareSkuQueryBo bo) {
        return iMallWareSkuService.queryPageList(bo);
    }

    /**
     * 导出商品库存列表
     */
    @ApiOperation("导出商品库存列表")
    @PreAuthorize("@ss.hasPermi('mall:wareSku:export')")
    @Log(title = "商品库存", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallWareSkuVo> export(@Validated MallWareSkuQueryBo bo) {
        List<MallWareSkuVo> list = iMallWareSkuService.queryList(bo);
        ExcelUtil<MallWareSkuVo> util = new ExcelUtil<MallWareSkuVo>(MallWareSkuVo.class);
        return util.exportExcel(list, "商品库存");
    }

    /**
     * 获取商品库存详细信息
     */
    @ApiOperation("获取商品库存详细信息")
    @PreAuthorize("@ss.hasPermi('mall:wareSku:query')")
    @GetMapping("/{id}")
    public AjaxResult<MallWareSkuVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iMallWareSkuService.queryById(id));
    }

    /**
     * 新增商品库存
     */
    @ApiOperation("新增商品库存")
    @PreAuthorize("@ss.hasPermi('mall:wareSku:add')")
    @Log(title = "商品库存", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallWareSkuAddBo bo) {
        return toAjax(iMallWareSkuService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品库存
     */
    @ApiOperation("修改商品库存")
    @PreAuthorize("@ss.hasPermi('mall:wareSku:edit')")
    @Log(title = "商品库存", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallWareSkuEditBo bo) {
        return toAjax(iMallWareSkuService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除商品库存
     */
    @ApiOperation("删除商品库存")
    @PreAuthorize("@ss.hasPermi('mall:wareSku:remove')")
    @Log(title = "商品库存" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iMallWareSkuService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
